package com.sense.kukoo.common.anim

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.sense.kukoo.lib_logger.LoggerManager

class TopTabAnimHelper {

    private val TAG = TopTabAnimHelper::class.simpleName
    private val defAnimateDuration = 400L

    private var mMinTranslationY:Float = 0F
    private var mFlagScrolling: Boolean? = null
    private var mMoveY:Float = 0F
    private lateinit var tabView: View

    fun setTabView(v:View){
        tabView = v
    }

    fun setMinTranslationY(max:Float){
        mMinTranslationY = max
    }

    fun animateToShow(){
        val translationY: Float = tabView.translationY
        if(translationY > -16F){
            tabView.translationY = 0F
            return
        }
        val duration = (translationY * defAnimateDuration) / mMinTranslationY
        tabView.animate()
            .translationY(0F)
            .setDuration(duration.toLong())
            .start()
    }

    fun animateToHide(){
        val translationY: Float = tabView.translationY
        val moveY =  mMinTranslationY - translationY
        if(moveY > -16F){
            tabView.translationY = mMinTranslationY
            return
        }
        val duration = (moveY * defAnimateDuration) / mMinTranslationY
        tabView.animate()
            .translationY(mMinTranslationY)
            .setDuration(duration.toLong())
            .start()
    }

    fun dispatchScrollEvent(moveY:Int, scrollState:Int){
        var translationY: Float = tabView.translationY
        translationY = translationY - moveY
        translationY = if(translationY < mMinTranslationY) mMinTranslationY else translationY
        translationY = if(translationY > 0) 0F else translationY
        tabView.translationY = translationY
        //
        mMoveY += moveY
        //
        if(mFlagScrolling == null){
            mFlagScrolling = true
            mMoveY = 0F
        } else {
            if(scrollState == RecyclerView.SCROLL_STATE_IDLE){
                mFlagScrolling = null
                animateToShow()
            }
        }
    }

}